package com.yaodian100.crm.activities.service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.transaction.annotation.Transactional;

import com.yaodian100.core.common.service.BaseManager;
import com.yaodian100.core.form.entity.Attribute;
import com.yaodian100.crm.activities.entity.Activities;
import com.yaodian100.crm.activities.entity.ActivitiesValue;

/**
 * @author yanling.wu
 * 
 */
public class ActivitiesServiceImpl implements ActivitiesService {
	@Resource
	private BaseManager baseMgr;

	/**
	 * 获取表单一一赋值
	 * 
	 * @param orderId 订单编号
	 * @param account 账号
	 * @param area 区域
	 * @param server 服务器
	 * @param userName 用户名
	 * @param mobileNo 手机号
	 */
	@Override
	@Transactional(readOnly = false)
	public Activities createActivities(String orderId, String account, String area, String server, String userName,
			String mobileNo) {
		String questionTypeName = "chdActivity";
		Object[] obj = baseMgr.queryByHql("from ActivitiesValue where value=?", new Object[] { orderId });

		if (obj.length > 0) {
			return null;
		} else {

			Map<String, String> values = new HashMap<String, String>();
			values.put("orderId", orderId);
			values.put("account", account);
			values.put("area", area);
			values.put("server", server);

			Activities activities = createactivitiesBase(questionTypeName, values, userName, mobileNo);
			return activities;

		}
	}

	/**
	 * 判断是否有此订单
	 * 
	 * @param userName 用户名
	 * @param orderId 订单编号
	 * @return
	 */
	@Override
	public String validationOrderId(String userName, String orderId) {
		Object[] oderObj = baseMgr.queryByHql("from Orders where memberId=? and orderId=?", new Object[] { userName,
				orderId });
		if (oderObj.length > 0) {
			return "success";
		}
		return "error";

	}

	/**
	 * 把表单数据保存到数据库
	 * 
	 * @param activitiesId 活动的ID
	 * @param activitiesTypeName 活动的名称
	 * @param values 活动Map
	 * @param userName 用户名
	 * @param mobileNo 手机号
	 * @return
	 */
	public Activities createactivitiesBase(String activitiesTypeName, Map<String, String> values, String userName,
			String mobileNo) {
		Object[] type = baseMgr.queryByHql("from Attribute where form.name=?", new Object[] { activitiesTypeName });
		Activities activities = new Activities();
		activities.setCreateUser(userName);
		activities.setMobileNo(mobileNo);
		Date createDT = new Date();
		activities.setCreateDate(createDT);
		baseMgr.save(activities);
		Map<String, ActivitiesValue> valueMap = new HashMap<String, ActivitiesValue>();
		for (Object obje : type) {
			Attribute attribute = (Attribute) obje;
			if (attribute.getDataType().equals("Button")) {
				continue;
			}
			ActivitiesValue av = new ActivitiesValue();
			av.setActivities(activities);
			av.setName(attribute.getName());
			av.setAttribute(attribute);
			for (String key : values.keySet()) {
				if (av.getName().equals(key)) {
					Object obj = values.get(key);
					String value = null;
					if (obj instanceof String[]) {
						value = ((String[]) obj)[0];
					} else {
						value = (String) obj;
					}
					av.setValue(value);
				}
			}
			baseMgr.save(av);
			valueMap.put(av.getName(), av);
		}
		activities.setAtValue(valueMap);
		return activities;

	}
}
